/**
 * @class   wosh::gui::IWidget
 * @brief   Base class for most WOSH GUI Widgets.
 *          
 *
 *
 ****************************************************************************
 * @class   wosh::gui::IWidgetGeneric
 * @brief   Extends QWidget and wosh::gui::IWidget. 
 *          Provided for convenience.
 *
 ****************************************************************************
 * @version $Id: IWidget.h 2644 2010-06-11 01:36:41Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_IWidget_H__
 #define __WOSH_Gui_IWidget_H__

 #include <woshDefs.h>
 #include <core/Reflection.h>
 #include <core/BusConnector.h>
 #include <core/URI.h>
 #include <core/Request.h>
 #include <QtGui/QWidget>


class QSignalMapper;

namespace wosh {
 class Response;
 namespace gui {


class IWidget :		public virtual IBusListener,
					public virtual IReflection
 {
	WOSH_CLASSNAME(wosh::gui::IWidget)
	WOSH_CLASS_ABSTRACT(true)


	public:
		IWidget() { }

		virtual WRESULT init();

		virtual ~IWidget() { }


/** @name IWidget interface
 * @{
 ******************************************************************************/
	public:
		virtual QWidget* asQWidget() = 0;

		virtual void refreshAll() = 0;

//@}
/** @name Getters and Setters
 * @{
 ******************************************************************************/
	public:
		virtual void setTargetURI( const URI& uri )			{ this->targetURI = uri; }
		inline void setTargetURI( const std::string& uri)	{ this->targetURI.fromString(uri); }

		virtual URI& getTargetURI()					{ return this->targetURI; }
		virtual const URI& getTargetURI() const 	{ return this->targetURI; }

		virtual void setWidgetURI( const URI& uri )	{ this->myURI = uri; }

		virtual URI& getWidgetURI()					{ return this->myURI; }
		virtual const URI& getWidgetURI() const 	{ return this->myURI; }

//@}
/** @name Internal methods provided for convenience
 * @{
 ******************************************************************************/
	protected:

		inline bool isMessageForMeOrFromTarget( const Message& message ) {
			if ( message.isEmpty() ) return false;
			if ( URI::pregMatch( this->targetURI, message.getSource() ) )
				return true;
			if ( URI::pregMatch( this->myURI, message.getDestination() ) )
				return true;
			return false;
		 }

		virtual void postMessageForTarget( Message* message );

//@}
/** @name Internal fields
 * @{
 ******************************************************************************/
	protected:
		BusConnector BusGui;	///< wosh.Bus.Gui connector, note that bus is a wosh::gui::BusRingQt
								///< so widget may update gui from busMessage() call within same thread
		URI myURI;				///< URI of this widget (mostly used with requests and to get responses)
		URI targetURI;			///< URI of target object (the object controlled by this widget)

//@}


}; // class def




class IWidgetGeneric :	public QWidget,
						public IWidget
 {
	Q_OBJECT

	WOSH_CLASSNAME(wosh::gui::IWidgetGeneric)
	WOSH_CLASS_ABSTRACT(false)


	public:
		IWidgetGeneric(QWidget *parent = 0 );

		virtual ~IWidgetGeneric();

	public:
		virtual QWidget* asQWidget()			{ return this; }

		void mapMethodButton( const std::string& method_name, QObject* sender_button );

	public slots:
		inline void executeRemoteMethod( const QString& remote_method ) {
			Request* request_method = new Request( remote_method.toLatin1().data() );
			postMessageForTarget( new Message(request_method) );
		 }
		inline void executeRemoteMethod( const QString& remote_method, const Variant& argument ) {
			Request* request_method = new Request( remote_method.toLatin1().data(), new Variant(argument) );
			postMessageForTarget( new Message(request_method) );
		 }
		inline void executeRemoteMethod( const QString& remote_method, ISerializable* argument ) {
			Request* request_method = new Request( remote_method.toLatin1().data(), argument );
			postMessageForTarget( new Message(request_method) );
		 }

	public slots:
		bool promptMethodResponse( const wosh::Message& message, bool filter_mapped = false, bool filter_failed = true, bool filter_target = true );

		void postMessage( Message* message ) {
			postMessageForTarget( message );
		 }

	protected:
		QSignalMapper* signalMapperBtn;


}; // class def



 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_IWidget_H__
